package lock;

public class Test3 {
    public static void main(String[] args) {
        TicketTask3 t1 = new TicketTask3();
        TicketTask3 t2 = new TicketTask3();
        TicketTask3 t3 = new TicketTask3();
        t1.setName("窗口一");
        t2.setName("窗口二");
        t3.setName("窗口三");
        t1.start();
        t2.start();
        t3.start();

    }
}
class TicketTask3 extends Thread {


    private static int tickets = 300;
    @Override
    public void run() {
        while (true) {
            //用类的字节码对象（唯一的，保证使用同一个对象的锁）
            synchronized (TicketTask3.class) {
                if (tickets<=0) {
                    break;
                }
                System.out.println(super.getName()+"卖出了第"+tickets+"张票");
                tickets--;
            }
        }
    }
}
